<?php
namespace Kxdr\Model;
use Think\Model\ViewModel;
class EventUserGiftViewModel extends ViewModel {
	public $sid;
	public $eid;
	public $uid;
	public $wharr;
   public $viewFields = array(
	 'user_gift'=>array('_table'=>'sm_kxdr_user_gifts','id'=>'user_gift_id', 'eid', 'egid','epid','guess_id','user_id','gift_id','player_id','send_time','is_picup','picup_time','gift_num','gift_type','get_type','user_nick','gift_name','gift_pic_url','_type'=>'LEFT'),
	 'guess_user'=>array('_table'=>'sm_kxdr_user_guess','sid','player_id','guess_type','guess_value','mixed_nick','guess_time','guess_value_difference','_on'=>'guess_user.id=user_gift.guess_id','_type'=>'LEFT'),
	 'e_player'=>array('_table'=>'sm_kxdr_event_players','id'=>'epid','title','think_type','think_type_way','think_type_sign','target_data','event_id','player_id','player_descrip'=>'event_player_descrip','_on'=>'e_player.id=user_gift.epid','_type'=>'LEFT'),
	 'player'=>array('_table'=>'sm_kxdr_players','id','chinese_name'=>'player_name','description'=>'player_descrip','is_team','team_id','_on'=>'e_player.player_id=player.id','game_category','_type'=>'LEFT'),
	 'sendlog'=>array('_table'=>'sm_gifts_sendlog','id'=>'send_id','send_time'=>"item_send_time",'is_auto_send','sender_nick','address','true_name','mobile','version','_on'=>'sendlog.ugid=user_gift.id','game_category','_type'=>'LEFT'),
     );
   public function getEventGuessUser(){
	    // unset($this->viewFields["player"]);
		$players = $this->where($this->wharr)->limit($this->lmtstr)->getField("user_gift_id,user_id,guess_id,epid,player_name,event_player_descrip,title,think_type,think_type_way,guess_type,guess_value,guess_time,guess_value_difference,mixed_nick,gift_name,gift_type,send_id,send_time,item_send_time");
		foreach($players as $k=>$p){
			foreach(C("KXDR_THINK_WAY") as $way){
				if($p["think_type_way"]==$way["id"]){
					$players[$k]["think_type_way_name"]=$way["name"];break;
				}
			}
			if($p["guess_value_difference"]==0){
				$players[$k]["gvdmsg"] = "完全猜中";
			}elseif($p["guess_value_difference"]<0){
				$players[$k]["gvdmsg"] = "没猜中";
			}else{
				$players[$k]["gvdmsg"] = "差".$p["guess_value_difference"]."分猜中";
			}
			
		}
		return $players;
   }
   public function getTotal(){
	   return $this->where($this->wharr)->limit($this->lmtstr)->count();
   }
   public function send_address_download(){
	   $this->viewFields["address"]=array('_table'=>'sm_user_address','nationality','province','city','city_part','address','name','tel','_on'=>'address.kxdr_uid=user_gift.user_id','_type'=>'LEFT');
		$data = $this->where($this->wharr)->limit($this->lmtstr)->getField("user_gift_id,title,think_type,think_type_way,guess_time,guess_value_difference,mixed_nick,gift_name,gift_type,send_time,item_send_time,nationality,province,city,city_part,address,name,tel");
		foreach($data as $k=>$p){
			foreach(C("KXDR_THINK_WAY") as $way){
				if($p["think_type_way"]==$way["id"]){
					$data[$k]["think_type_way_name"]=$way["name"];break;
				}
			}
			if($p["guess_value_difference"]==0){
				$data[$k]["gvdmsg"] = "完全猜中";
			}elseif($p["guess_value_difference"]<0){
				$data[$k]["gvdmsg"] = "没猜中";
			}else{
				$data[$k]["gvdmsg"] = "差".$p["guess_value_difference"]."分猜中";
			}
			foreach(C("GFT_TYPE") as $gtp){
				if($gtp["id"]==$p["gift_type"]){
					$data[$k]["gift_type"] = $gtp["name"];
				}
			}
		}
		if($this->wharr["gift_type"]==1){
			$str = $this->load_address_str($data);
		}else{
			$str = $this->load_gifts_str($data);
		}
		$filename = "中奖信息".date('Ymd').'.csv'; //设置文件名   
		// header("Content-type: application/vnd.ms-excel; charset=gb2312" );
		header("Content-type:text/csv");   
		header("Content-Disposition:attachment;filename=".$filename);   
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
		header('Expires:0');   
		header('Pragma:public');
		
		echo mb_convert_encoding($str,"gb2312","utf-8");  
	}
	private function load_address_str($data){
		$str = "业务单号,收件人姓名,收件人手机,收件省,收件市,收件区/县,收件人地址,奖品名,数量,备注\r\n";   
		foreach($data as $k=>$gift){
			$str.=$data[$k]["user_gift_id"].","
				 .$data[$k]["name"].","
				 .$data[$k]["tel"].","
				 .$data[$k]["province"].","
				 .$data[$k]["city"].","
				 .$data[$k]["city_part"].","
				 .$data[$k]["address"].","
				 .$data[$k]["gift_name"].","
				 ."1,"
				 .date("Y-m-d H:i:s",strtotime($data[$k]["guess_time"])).$data[$k]["gvdmsg"].","
				 ."\r\n";
		}
		return $str;
	}

	private function load_gifts_str($data){
		$str = "业务单号,预测标题,猜什么,怎么猜,猜分结果,奖品名称,奖品类型,领奖时间,实物奖发奖时间,地址,收货人,电话,备注\r\n";   
		foreach($data as $k=>$gift){
			$str.=$data[$k]["user_gift_id"].","
				 .$data[$k]["title"].","
				 .$data[$k]["think_type"].","
				 .$data[$k]["think_type_way_name"].","
				 .$data[$k]["gvdmsg"].","
				 .$data[$k]["gift_name"].","
				 .$data[$k]["gift_type"].","
				 .date("Y-m-d H:i:s",strtotime($data[$k]["send_time"])).","
				 .($data[$k]["item_send_time"]?date("Y-m-d H:i:s",strtotime($data[$k]["item_send_time"])):"").","
				 .($data[$k]["item_send_time"]?$data[$k]["province"].$data[$k]["city"].$data[$k]["city_part"].$data[$k]["address"]:"").","
				 .($data[$k]["item_send_time"]?$data[$k]["name"]:"").","
				 .($data[$k]["item_send_time"]?$data[$k]["tel"]:"").","
				 ."\r\n";
		}
		return $str;
	}
}